package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {


    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee , HttpServletRequest request) {
        log.info("用户开始登录了,登录信息:"+employee);
        log.info("跑通前后端不管业务....");
        //加密//用户名录入密码
        String username = employee.getUsername();
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());


        //根据用户名查询数据库 该用户信息 返回一个员工
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,username);
        Employee one = employeeService.getOne(wrapper);
        //是null登录失败
        if (one==null) {
            return R.error("用户名或密码不存在");
        }
        //到这说明有对象
        //进行密码判断
        if (!one.getPassword().equals(password)){//密码不一样
            return R.error("用户名或密码不存在");
        }
        //到这说明有对象密码正确
        //判断状态
        if (one.getStatus()==0) {//被封了
            return R.error("账号以禁用，联系客服");
        }
        //全对，没有封号
        //把用户ID存到session中
        request.getSession().setAttribute("employee",one.getId());
        log.info("员工登录成功");
        return R.success(one);

    }
    @PostMapping("logout")
    public R<String> logout(HttpSession session) {
        //退出登录
        log.info("员工正在退出登录");
        session.removeAttribute("employee");
        //销毁session
        session.invalidate();
        return R.success("退出成功");

    }


    /**
     * 添加员工
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee ,HttpSession session) {
        //1.通过日志打印收到的数据
        log.info("员工基本信息" + employee);
        log.info("[员工基本信息:{}],employee");
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        // 把 123456 字符串转换字节 进行md5加密
        employee.setPassword(password);
        LocalDateTime localDateTime = LocalDateTime.now();
        employee.setCreateTime(localDateTime);
        employee.setUpdateTime(localDateTime);
        //当前登录的用户
        Long empId = (Long) session.getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("添加成功");

    }
}
